package loengud.neljas;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 * Reads file and reverses all the lines.
 * @author ago
 *
 */
public class Reverse {

	/**
	 * Count of numbers to be read.
	 */
	private static final int BUFFER_SIZE = 100;
	/**
	 * Input file to be read.
	 */
	private static final String INPUT_FILE = "src/loengud/neljas/numbrid.txt";
	
	/**
	 * Numbers from file.
	 */
	private static int[] numbers;
	/**
	 * @param args 
	 */
	public static void main(String[] args) {
		numbers = new int[BUFFER_SIZE];
		readFile();
	}
	
	private static void readFile() {
		// declare scanner here to use it outside try-block
		Scanner sc = null;
		int numberCount = 0;
		try {
			FileReader fr = new FileReader(INPUT_FILE);
			System.out.println("fail on olemas");
			//BufferedReader br = new BufferedReader(fr);
			//int b = fr.read();
			//System.out.println(b);
			//String line = br.readLine();
			//System.out.println(line);
			
			sc = new Scanner(fr);
			int i;
			
			while (true) {
				if (sc.hasNextInt()) {
					i = sc.nextInt();
					numbers[numberCount++] = i;
				} else {
					sc.next();
				}
				//System.out.println(i);
			}
		} catch (NoSuchElementException e) {
			// file ended?
			// breaks while (true)
			sc.close();
			//System.out.println("no more input");
		} catch (FileNotFoundException e) {
			System.out.println("File not found");
			//e.printStackTrace();
		} catch (IOException e) { // needed for br.readLine();
			e.printStackTrace();
		}
		// let's print out numbers in reverse order
		for (int i = numberCount - 1; i >= 0; i--) {
			System.out.println(numbers[i]);
		}
	}

}
